import 'url-polyfill';

export class RuntimeUtils {
    static get instance() {
        return RuntimeUtils._instance;
    }

    get obvProtocol() {
        return this.serviceProtocol;
    }

    get isServiceHttps() {
        return this.serviceProtocol === 'https:';
    }

    get obvJsPath() {
        return `${this.obvProtocol}//${this.obvEnv}${this.viewerPath}/obv.js`;
    }

    get obvCssPath() {
        return `${this.obvProtocol}//${this.obvEnv}${this.viewerPath}/obv.css`;
    }

    get obvPropertiesPath() {
        return `${this.obvProtocol}//${this.obvEnv}${this.viewerPath}/locale/locale.properties`;
    }

    get bimboxOrigin() {
        return this.origin;
    }

    static init(envParams) {
        RuntimeUtils._instance = new RuntimeUtils(envParams);
    }

    static _instance;
    origin = '';
    obvOrigin = '';
    viewerPath = '';
    obvEnv = '';
    serviceProtocol = '';

    constructor(envParams) {
        const bimboxUrl = new URL(envParams.bimboxOrigin);
        const serviceUrl = new URL(envParams.serviceOrigin);

        this.origin = bimboxUrl.origin;
        this.viewerPath = '/bimviewer/viewer/v4';
        this.obvEnv = serviceUrl.host;
        this.obvOrigin = serviceUrl.origin;
        this.serviceProtocol = serviceUrl.protocol;
    }
}
